#include <stdio.h>

#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"

#include "globals.h"
#include "keypad.h"

/* Flags */
char keyp_base_left     = DISABLED;
char keyp_base_right    = DISABLED;
char keyp_shoulder_up   = DISABLED;
char keyp_shoulder_down = DISABLED;
char keyp_elbow_up      = DISABLED;
char keyp_elbow_down    = DISABLED;
char keyp_gripper_close = DISABLED;
char keyp_home          = DISABLED;
char keyp_record        = DISABLED;
char keyp_play          = DISABLED;
char keyp_record_end    = DISABLED;
char keyp_test          = DISABLED;

/* Ensure the array is clear */
void ClearColumns(alt_u8 * keypad_buttons)
{
    int i;
    for (i = 0; i < NUM_COLUMNS; i++)
    {
        keypad_buttons[i] = 0;
    }
}

/* Set global flags */
int AssignButtonValues(const alt_u8 * keypad_buttons)
{
    keyp_base_left      = (keypad_buttons[1] == 4) ? ENABLED : DISABLED;
    keyp_base_right     = (keypad_buttons[2] == 4) ? ENABLED : DISABLED;
    
    keyp_elbow_up       = (keypad_buttons[3] == 3) ? ENABLED : DISABLED;
    keyp_elbow_down     = (keypad_buttons[3] == 4) ? ENABLED : DISABLED;
    
    keyp_shoulder_up    = (keypad_buttons[0] == 3) ? ENABLED : DISABLED;
    keyp_shoulder_down  = (keypad_buttons[0] == 4) ? ENABLED : DISABLED;
    
    keyp_gripper_close  = (keypad_buttons[2] == 3) ? ENABLED : DISABLED;
    
    keyp_home           = (keypad_buttons[1] == 3) ? ENABLED : DISABLED;
    
    keyp_record         = (keypad_buttons[0] == 1) ? ENABLED : DISABLED;
    keyp_play           = (keypad_buttons[1] == 1) ? ENABLED : DISABLED;
    keyp_record_end     = (keypad_buttons[2] == 1) ? ENABLED : DISABLED;
    keyp_test           = (keypad_buttons[3] == 1) ? ENABLED : DISABLED;
    
    return (keyp_base_left | keyp_base_right | keyp_elbow_down | 
            keyp_elbow_up | keyp_gripper_close | keyp_home | keyp_play | 
            keyp_record | keyp_record_end | keyp_shoulder_down |
            keyp_shoulder_up | keyp_test);
}

int ReadKeypad(void)
{
    /* variables */
    alt_u8 keypad_buttons[NUM_COLUMNS];
    alt_u8 active_row = 0;
    alt_u8 key_out;
    alt_u8 column_select;
    char column, i;
    char active_column = -1;
    
    ClearColumns(keypad_buttons);
    
    /* Read each column */
    for (column = COLUMN_MIN; column < NUM_COLUMNS; column++)
    {
        column_select = (0x0F) & ~(1 << column);
            
        /* Select next Column */
        IOWR_ALTERA_AVALON_PIO_DATA(KEYOUT_BASE, column_select);
        
        /* read active key */
        key_out = IORD_ALTERA_AVALON_PIO_DATA(KEYIN_BASE);
        if (key_out == 0)
        {
            active_column = COLUMN_MAX - column;
        }
        else
        {
            /* Convert active row from non-active column to row number */
            if (active_row < 1)
            {
                active_row = 0;
                for (i = key_out; i > 0; i >>= 1)
                {
                    active_row++;
                }
            }
        }
        
        /* If we know the row and column, set the array index */
        if ((active_column > -1) && (active_row > 0))
        {
            keypad_buttons[(int)active_column] = (alt_u8)active_row;
        }
    }
    
    return AssignButtonValues(keypad_buttons);
}

char PlaybackKey(void)
{
    if (keyp_play)
    {
        return 1;
    }
    
    return 0;
}
